//加载数据库
//需要4个基本信息，即数据库驱动类（driverClass），数据库地址（url），用户名（user），密码（password）

public Connection getConnection(String driverClass, String url, Properties info) throws Exception {
    //加载驱动
    Class clazz = Class.forName(driverClass);
    Driver driver = (Driver) clazz.getDeclaredConstructor().newInstance();
    //返回连接
    return driver.connect(url, info);
}

public Connection getConnection(String driverClass, String url, String user, String password) throws Exception {
    //加载驱动
    Class clazz = Class.forName(driverClass);
    Driver driver = (Driver) clazz.getDeclaredConstructor().newInstance();
    DriverManager.registerDriver(driver);
    //返回连接
    return DriverManager.getConnection(url, user, password);
}

//平常使用版
public Connection getConnection(String driverClass, String url, String user, String password) throws Exception {
    //加载驱动
    Class.forName(driverClass);
    //返回连接
    return DriverManager.getConnection(url, user, password);
}

//高级使用版
public Connection getConnection(String propertiesFile) throws Exception {
    //获取配置文件
    FileInputStream fis = new FileInputStream(propertiesFile);
    //键值对读取信息
    Properties p = new Properties();
    p.load(fis);
    String driverClass = p.getProperty("driverClass");
    String url = p.getProperty("url");
    String user = p.getProperty("user");
    String password = p.getProperty("password");
    //加载驱动
    Class.forName(driverClass);
    //返回连接
    return DriverManager.getConnection(url, user, password);
}